package com.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.entity.Expert;
import com.demo.mapper.ExpertMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *
 */
@RestController
@RequestMapping("/expert")
public class ExpertController {
  /**
   * 服务对象
   */
  @Resource
  ExpertMapper expertMapper;
  
  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Expert expert) {
    expertMapper.insert(expert);
    return Result.success();
  }
  
  //  删除 ById
  @DeleteMapping("/{id}")
  public Result<?> delete(@PathVariable Integer id) {
    expertMapper.deleteById(id);
    return Result.success();
  }
  
  // 修改
  @PutMapping
  public Result<?> updata(@RequestBody Expert expert) {
    expertMapper.updateById(expert);
    return Result.success();
  }
  
  /**
   * 分页,默认1页,10个数据,  根据 Ename(姓名) 查询
   *
   * @param pageNum  页数
   * @param pageSize 页面显示数
   * @param search   搜索关键词
   * @return
   */
  @GetMapping
  public Result<?> findPageByaName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search) {
    LambdaQueryWrapper<Expert> wrapper = Wrappers.<Expert>lambdaQuery();
    
    if (StrUtil.isNotBlank(search)) {
          wrapper.like(Expert::getEname, search);
    }
    
    Page<Expert> AdminPage = expertMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(AdminPage);
  }
  
  
}

